<template>
    <t-dialog v-model:visible="visible" header="选择物料" width="50%" :confirm-on-enter="true"
        :on-confirm="onConfirmAnother" :confirmLoading="loading">
        <t-form label-align="right" label-width="80px">
            <t-row :gutter="12">
                <t-col :span="12" class="form_col">
                    <t-form-item label="父分组内码" name="parentId" class="form_item">
                        <t-tree-select :value="parentId" :data="materGroupList" clearable placeholder="请选择" readonly />
                    </t-form-item>
                </t-col>
                <t-col :span="12" class="form_col">
                    <t-form-item label="分组名称" name="FName" class="form_item">
                        <t-input v-model="formData.FName" placeholder="请输入名称" />
                    </t-form-item>
                </t-col>

                <t-col :span="12" class="form_col">
                    <t-form-item label="分组编码" name="FNumber" class="form_item">
                        <t-input v-model="formData.FNumber" placeholder="请输入名称" />
                    </t-form-item>
                </t-col>

            </t-row>
        </t-form>
    </t-dialog>
</template>

<script>
    import { ref, onMounted, reactive } from 'vue'
    import { queryMaterialGroupList,addMaterialGroup } from '@/service/material';
    import { MessagePlugin } from 'tdesign-vue-next';

    export default {
        props: ['parentId'],
        setup(props,{emit}) {
            const visible = ref(false);
            const loading = ref(false);
            
            const materGroupList = ref([]);
            const formData = reactive({})

            const loadMaterGroup = async () => {
                const res = await queryMaterialGroupList({});
                if (res) {
                    materGroupList.value = res.dataList;
                }
            }


            onMounted(() => {
                loadMaterGroup();
            });

            const onConfirmAnother= async()=>{
                if(!formData.FName){
                    MessagePlugin.error('请填写名称');
                    return
                }
                loading.value = true
                const res = await addMaterialGroup({...formData,'FParentId':props.parentId})
                if(res.success){
                    emit('onSaved')
                    visible.value = false
                }else{
                    MessagePlugin.error('保存错误:'+res.msg);
                }
                loading.value = false

            }


            const openGroupDialog = () => {
                visible.value = true
            }

            return { visible, loading,materGroupList, formData, openGroupDialog,onConfirmAnother }


        }
    }

</script>


<style>
    .form_col {
        margin-top: 10px;
    }
</style>